local assets =
{
    -- Asset("ANIM", "anim/panda_fisherman_supply_pack.zip"),
    -- Asset( "IMAGE", "images/inventoryimages/panda_fisherman_supply_pack.tex" ),  -- 背包贴图
    -- Asset( "ATLAS", "images/inventoryimages/panda_fisherman_supply_pack.xml" ),
}
local function GetStringsTable()
    return TUNING.NPC_CHAT_TABLE[ type(TUNING.__NPC_LANGUAGE)=="function" and TUNING.__NPC_LANGUAGE() or TUNING.__NPC_LANGUAGE ] ["npc_item_wortox_hambat"] or {}
end

local function smoke_up(inst,owner)
    if inst.fires == nil then
        inst.fires = {}
        owner.SoundEmitter:PlaySound("dontstarve/wilson/torch_swing")
        local fx = SpawnPrefab("torchfire_spooky")
        fx.entity:SetParent(owner.entity)
        fx.entity:AddFollower()
        fx.Follower:FollowSymbol(owner.GUID, "swap_object", fx.fx_offset_x or 0, fx.fx_offset, 0)
        if fx.AssignSkinData ~= nil then
            fx:AssignSkinData(inst)
        end
        table.insert(inst.fires, fx)
    end
end
local function smoke_end(inst,owner)
    if inst.fires ~= nil then
        for i, fx in ipairs(inst.fires) do
            fx:Remove()
        end
        inst.fires = nil
        owner.SoundEmitter:PlaySound("dontstarve/common/fireOut")
    end
end

local function onequip(inst, owner)
    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("equipskinneditem", inst:GetSkinName())
        owner.AnimState:OverrideItemSkinSymbol("swap_object", skin_build, "swap_ham_bat", inst.GUID, "swap_ham_bat")
    else
        owner.AnimState:OverrideSymbol("swap_object", "swap_ham_bat", "swap_ham_bat")
    end

    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")

    if inst.components.finiteuses.current < 20 and  owner.npc_base_lib then
        smoke_up(inst,owner)
        owner.npc_base_lib:Say(GetStringsTable().go_worng)
    end

end

local function onunequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
    local skin_build = inst:GetSkinBuild()
    if skin_build ~= nil then
        owner:PushEvent("unequipskinneditem", inst:GetSkinName())
    end
    smoke_end(inst,owner)
end


local function onattack_fn(inst, attacker, target)
    if target == nil or attacker == nil or attacker.components.playercontroller == nil then
        return
    end
    ----- 只有玩家才能触发
    local dmg = 20
    if attacker.components.hunger then
        dmg = ( attacker.components.hunger.max - attacker.components.hunger.current )*2
        -- attacker.components.hunger:DoDelta(-1)   --- UI太吵闹了
        attacker.components.hunger.current = math.clamp( attacker.components.hunger.current - 1 , 0,500)
    end
    dmg = math.ceil(math.clamp(dmg,5,500))
    
    if target and target.components.combat then
        target.components.combat:GetAttacked(attacker, dmg, inst)
    elseif target.components.health then
        target.components.health:DoDelta(-dmg)
    end
    inst.components.finiteuses:Use(0.5,true)
    if inst.components.finiteuses.current < 20 and not inst:HasTag("go_worng") then
        smoke_up(inst,attacker)
        inst:AddTag("go_worng")
        attacker.npc_base_lib:Say(GetStringsTable().go_worng)
    end
end

local function OnFinished_fn(inst)
    local pt = Vector3(inst.Transform:GetWorldPosition())
    local players = TheSim:FindEntities(pt.x, 0, pt.z, 5, {"player"}, {"playerghost"}, nil)
    for k, temp_player in pairs(players) do
        if temp_player and temp_player.components.sanity then
            if temp_player.components.sanity:GetPercent() > 0.05 then
                temp_player.components.sanity:SetPercent(0.05)
                temp_player.components.sanity:DoDelta(-1)
            end
        end
    end
    TheWorld.npc_base_lib:Announce(GetStringsTable().announce)
    inst:Remove()
end

local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("ham_bat")
    inst.AnimState:SetBuild("ham_bat")
    inst.AnimState:PlayAnimation("idle")

    --weapon (from weapon component) added to pristine state for optimization
    inst:AddTag("weapon")
    inst:AddTag("npc_item_skin")
    inst:AddTag("npc_no_random_skin")

    local swap_data = {sym_build = "swap_ham_bat", bank = "ham_bat"}
    MakeInventoryFloatable(inst, "med", nil, {1.0, 0.5, 1.0}, true, -13, swap_data)

    inst.entity:SetPristine()

    inst.prefab_base = "hambat"

    if not TheWorld.ismastersim then
        return inst
    end
    inst:AddComponent("npc_base_lib")
    inst:AddComponent("npc_everything_data")
    inst.npc_base_lib:Add_Reskin_Event()

    inst:AddComponent("inspectable") --可检查组件
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:ChangeImageName("hambat")
	inst.components.inventoryitem.cangoincontainer = true
    inst:AddComponent("named")
    -- inst.components.named:SetName(tostring(_table.Name))
    inst.components.named:SetName(GetStringsTable().name)
    STRINGS.CHARACTERS.GENERIC.DESCRIBE[string.upper("npc_item_wortox_hambat")] = GetStringsTable().inspect_str  --人物检查的描述

    ---------------------------------------------------------------------------------------------------
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(0)
    inst.components.weapon:SetOnAttack(onattack_fn)
    inst.components.weapon.attackrange = 1.17
    inst.components.weapon.hitrange = 1.17

    MakeHauntableLaunchAndPerish(inst)
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetOnFinished(OnFinished_fn)
    inst.components.finiteuses.Use = function(self,num,flag)
        if flag == true then
            self:SetUses(self.current - (num or 1))    
        end
    end

    return inst
end

return Prefab("npc_item_wortox_hambat", fn,assets)